package tree_util;

import com.common.FishEPTool;
import com.common.MoleculeTool;
import com.opensymphony.xwork2.ActionSupport;
import db_tool.LogTool;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 10742 on 2017/7/9.
 */
public class Op_Molecule extends ActionSupport {

    int id;
    int accountID;
    String software;
    String document;
    String sampleID;
    String flag;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAccountID() {
        return accountID;
    }

    public void setAccountID(int accountID) {
        this.accountID = accountID;
    }

    public String getSoftware() {
        return software;
    }

    public void setSoftware(String software) {
        this.software = software;
    }

    public String getDocument() {
        return document;
    }

    public void setDocument(String document) {
        this.document = document;
    }

    public String getSampleID() {
        return sampleID;
    }

    public void setSampleID(String sampleID) {
        this.sampleID = sampleID;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    @Override
    public String execute() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        MoleculeTool tool = (MoleculeTool) context.getBean("moleculeTool");
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        System.out.println(accountID + " " + software + " " + document + " " + sampleID);
        if (flag.equals("insert")) {
            id = tool.insert(accountID, software, document, sampleID);
            LogTool.insertIntoDataLog(LogTool.INSERT_CODE, "MolecuMaker_Resear", 1);
            writer.write(String.valueOf(id));
            writer.close();
        } else if (flag.equals("update")) {
            int column = tool.update(id, accountID,software, document);
            if (column == 1){
                LogTool.insertIntoDataLog(LogTool.UPDATE_CODE, "MolecuMaker_Resear", 1);
                writer.write("true");
            }
            writer.close();
        }
        return null;
    }

}
